import 'package:flutter/material.dart';
import 'package:flutterproject/mvvm/base/base_controller.dart';
import 'package:flutterproject/page/setting/children/child_account/data/child_account_list_bean.dart';
import 'package:flutterproject/util/address_manager.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/extension/widget_extension.dart';
import 'package:flutterproject/util/http/http_client.dart';
import 'package:flutterproject/util/http/http_exception.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/button_common.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:flutterproject/widget/text/text_field_common.dart';
import 'package:get/get.dart';

class ChildAccountController extends BaseController {
  HttpClient client = Get.find<HttpClient>();
  ChildAccountListBean? childAccountListBean = ChildAccountListBean();
  final mobilController = TextEditingController();

  @override
  void onReady() {
    super.onReady();
    getChildAccountList();
  }

  Future getChildAccountList() async {
    try {
      DialogUtil.showLoading();
      childAccountListBean =
          await client.get<ChildAccountListBean>(AddressManager.childAccountList);
      update();
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }

  Future addChildAccount(String mobile) async {
    try {
      DialogUtil.showLoading();
      await client.post<ChildAccountListBean>(AddressManager.addChildAccount,
          data: {"mobile": mobile});
      ToastUtil.showSuccess("绑定成功");
      await DialogUtil.dismiss();
      getChildAccountList();
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }

  void showDialog() {
    DialogUtil.showCustomDialog(
        Container(
          margin: MarginPaddingUtil.symmetric(horizontal: 40),
          padding: MarginPaddingUtil.fromLTRB(
              left: 30, top: 39, right: 30, bottom: 69),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20.sp),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                children: [
                  TextCommon(
                    "绑定子账号",
                    size: 28.sp,
                    fontWeight: FontWeight.bold,
                  ),
                  Spacer(),
                  ImageCommon(
                    "/icon/icon_close",
                    size: 26.width,
                    fit: BoxFit.fill,
                  ).addClickEvent(() => DialogUtil.dismiss())
                ],
              ),
              31.heightBox,
              TextFieldCommon(
                controller: mobilController,
                width: Get.width,
                height: 120.width,
                backgroundColor: Color(0xFFF6F6F6),
                hintText: "输入需要绑定的子账号",
                hintColor: Color(0xFF707070),
                hintSize: 28.sp,
                size: 28.sp,
                textColor: Colors.black,
                borderRadius: 32.sp,
                padding: MarginPaddingUtil.symmetric(horizontal: 25),
              ),
              72.heightBox,
              ButtonCommon(
                width: 472.width,
                text: "确认绑定",
                onTap: () {
                  if (mobilController.text.length < 11) {
                    ToastUtil.showTips("请填写11位手机号");
                    return;
                  }
                  addChildAccount(mobilController.text);
                },
                fontSize: 32.sp,
                fontColor: Colors.white,
                color: Color(0xFFEDC88B),
                borderRadios: 30.sp,
                padding: MarginPaddingUtil.only(top: 25, bottom: 29),
              )
            ],
          ),
        ),
        alignment: Alignment(0, 0));
  }
}
